package com.polaris.live.dialog

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.DialogVipBuyBinding
import com.polaris.live.translation.R.string

/**
 * VipBuyDialog vip购买确认
 *
 * @author Created by 天晴 on 2024/12/16/025 11:35
 */
class VipBuyDialog(
    context: Context,
    private val level: Long,
    private val diamonds: Long,
    private val days: Long,
    private val ok: () -> Unit,
) : CenterPopupView(context) {

    private val vipMedalTitles = arrayOf(
        R.drawable.vip_medal_1, // 对应等级 0
        R.drawable.vip_medal_2, // 对应等级 1
        R.drawable.vip_medal_3, // 对应等级 2
        R.drawable.vip_medal_4, // 对应等级 3
        R.drawable.vip_medal_5, // 对应等级 4
        R.drawable.vip_medal_6  // 对应等级 5
    )
    private val bgColors = arrayOf(
        R.drawable.vip_bg_1, // 对应等级 0
        R.drawable.vip_bg_2, // 对应等级 1
        R.drawable.vip_bg_3, // 对应等级 2
        R.drawable.vip_bg_4, // 对应等级 3
        R.drawable.vip_bg_5, // 对应等级 4
        R.drawable.vip_bg_6  // 对应等级 5
    )
    private lateinit var mBinding: DialogVipBuyBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_vip_buy
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogVipBuyBinding.bind(popupImplView)
        mBinding.apply {
            val mLevel = (level - 1).toInt()
            if (mLevel in 0..5) {
                mBinding.vipLevelImg.setImageResource(vipMedalTitles[mLevel])
                mBinding.root.setBackgroundResource(bgColors[mLevel])
            } else {
                mBinding.vipLevelImg.setImageResource(vipMedalTitles[0])
                mBinding.root.setBackgroundResource(bgColors[0])
            }
            messageTv.text = StringUtils.getString(string.vip_by_days, level, days)

            val diamondStr = "$diamonds${StringUtils.getString(string.diamonds)}"
            val tipsStr = StringUtils.getString(string.you_sure_purchase_vip, diamondStr)
            val spannableString = SpannableString(tipsStr)
            val rewardIndex = tipsStr.indexOf(diamondStr)
            if (rewardIndex != -1) {
                spannableString.setSpan(
                    ForegroundColorSpan(ColorUtils.getColor(R.color.vip_color_ffc761)),
                    rewardIndex,
                    rewardIndex + diamondStr.length,
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                )
            }

            tipsTv.text = spannableString

        }
        mBinding.cancelTv.setOnClickListener {
            dismiss()
        }
        mBinding.okTv.setOnClickListener {
            dismiss()
            ok.invoke()
        }
    }

    override fun getMaxWidth(): Int {
        return 295.autoDp
    }

    override fun getPopupWidth(): Int {
        return 295.autoDp
    }

    companion object {

        fun show(
            context: Context,
            level: Long,
            diamonds: Long,
            days: Long,
            ok: () -> Unit,
        ) {

            XPopup.Builder(context)
                .isViewMode(false)
                .hasShadowBg(true)
                .hasNavigationBar(false)
                .moveUpToKeyboard(false)
                .navigationBarColor(R.color.color_transparent)
                .isDestroyOnDismiss(true)
                .asCustom(VipBuyDialog(context, level, diamonds, days, ok))
                .show()
        }
    }
}